package com.mszlu.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.dao.mapper.TagMapper;
import com.mszlu.blog.dao.pojo.Tag;
import com.mszlu.blog.service.TagService;
import com.mszlu.blog.vo.PageResult;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.TagVo;
import com.mszlu.blog.vo.params.TagPageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Package: com.mszlu.blog.service.impl
 * @ClassName: TagServiceImpl
 * @Author: 陈威
 * @CreateTime: 2021/8/27 17:46
 * @Description:
 */
@SuppressWarnings("all")
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<TagVo> findTagsByArticleId(Long articleId) {
        List<Tag> tags = tagMapper.findTagsByArticleId(articleId);
        return copyList(tags);
    }

    //查询最热标签  根据tagid查询文章数量最多的id  limt代表查询出几个
    @Override
    public Result hot(int limit) {
        List<Long> tagIds = tagMapper.findHotsTagIds(limit);
        if (CollectionUtils.isEmpty(tagIds)) {
            return Result.success(Collections.emptyList());
        }
        //需求是tagid和tagname  所以需要根据id查询出tagname
        List<Tag> tagList = tagMapper.findTagNameByTagIds(tagIds);
        return Result.success(tagList);
    }

    //获取全部的标签
    @Override
    public Result findAll() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId, Tag::getTagName);
        List<Tag> tags = tagMapper.selectList(queryWrapper);
        List<TagVo> list = copyList(tags);
        return Result.success(list);
    }

    //标签文章列表
    @Override
    public Result findDetailById(Long id) {
        Tag tag = tagMapper.selectById(id);
        TagVo tagVo = copy(tag);
        return Result.success(tagVo);
    }

    //业务逻辑
    @Override
    public Result getTagAll(TagPageParam params) {
        Page<Tag> page = new Page<>(params.getPageNo(), params.getPageSize());
        IPage<Tag> categoryIPage = tagMapper.getTagAll(page, params.getTagName(), params.getDescription());
        List<Tag> records = categoryIPage.getRecords();
        PageResult<TagVo> pageResult = new PageResult<>();

        List<TagVo> voList = copyList(records);
        if (records.size() > 0) {
            pageResult.setList(voList);
            pageResult.setTotal(categoryIPage.getTotal());
        }
        return Result.success(pageResult);
    }
    //业务逻辑
    @Override
    public Result deleteTagById(Long id) {
        if (id == null || id <= 0) {
            return Result.fail(999, "系统错误");
        }
        tagMapper.deleteById(id);

        return Result.success(null);
    }
    //业务逻辑
    @Override
    public Result UpdateTagById(Tag tag) {
        if (tag == null) {
            return Result.fail(999, "系统错误");
        }

        tagMapper.updateById(tag);
        return Result.success(null);
    }
    //业务逻辑
    @Override
    public Result addTag(Tag tag) {
        if (tag == null) {
            return Result.fail(999, "系统错误");
        }
        tag.setAvatar("/static/tag/css.png");
        tagMapper.insert(tag);
        return Result.success(null);
    }

    private List<TagVo> copyList(List<Tag> tags) {
        List<TagVo> list = new ArrayList<>();
        for (Tag tag : tags) {
            list.add(copy(tag));
        }
        return list;
    }

    private TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag, tagVo);
        tagVo.setId(String.valueOf(tag.getId()));
        return tagVo;
    }
}